package com.eiei.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.eiei.bean.MessageBean;
import com.eiei.common.CommonValue;

public class MessageBeanDaoImpl implements MessageBeanDao {
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");

	
	public boolean save(MessageBean message, Connection con) throws Exception {
		PreparedStatement stat = null;
		try{
			StringBuffer insert = new StringBuffer();
			insert.append(" insert into t_message(");
			insert.append("     id,");
			insert.append("     content,");
			insert.append("     sender,");
			insert.append("     reciver,");
			insert.append("     createdDate,");
			insert.append("     status");
			insert.append(" )");
			insert.append(" values(");
			insert.append("     ?,");
			insert.append("     ?,");
			insert.append("     ?,");
			insert.append("     ?,");
			insert.append("     ?,");
			insert.append("     ?) ");
			
			stat = con.prepareStatement(insert.toString());
			stat.setString(1, message.getId());
			stat.setString(2, message.getContent());
			stat.setString(3, message.getSender());
			stat.setString(4, message.getReciver());
			stat.setString(5, sdf.format(message.getCreateDate()));
			stat.setString(6, message.getStatus());
			stat.execute();
			
			return true;
		}catch(Exception e){
			throw e;
		}finally{
			if(stat!=null){
				if(!stat.isClosed()){
					stat.close();
				}
			}
		}
		
	}


	@Override
	public List<MessageBean> list(Integer fristIndex, Integer size, Connection con) throws Exception {

		PreparedStatement stat = null;
		ResultSet rs  = null;
		List<MessageBean> result = null;
		try{
			if(fristIndex == null || fristIndex < 1){
				fristIndex = 0;
			}else{
				fristIndex = fristIndex - 1;
			}
			
			if(size == null || size == 0){
				size = 20;
			}
			
			StringBuffer sql = new StringBuffer();
			sql.append(" select ");
			sql.append("     id, ");
			sql.append("     createdDate, ");
			sql.append("     content, ");
			sql.append("     sender, ");
			sql.append("     reciver, ");
			sql.append("     status ");
			sql.append(" from t_message ");
			sql.append(" order by createdDate desc ");
			sql.append(" limit ").append(fristIndex).append(",").append(size);
			
			stat = con.prepareStatement(sql.toString());
			rs = stat.executeQuery();
			result = new ArrayList<MessageBean>();
			while(rs.next()){
				java.sql.Date createdDate = rs.getDate("createdDate");
				java.util.Date date = new java.util.Date(createdDate.getTime()); 
				String content = rs.getString("content");
				String sender = rs.getString("sender");
				String reciver = rs.getString("reciver");
				String id = rs.getString("id");
				String status = rs.getString("status");
				MessageBean tmp = new MessageBean();
				tmp.setContent(content);
				tmp.setCreateDate(date);
				tmp.setSender(sender);
				tmp.setReciver(reciver);
				tmp.setId(id);
				tmp.setStatus(status);
				result.add(tmp);
			}
		}catch(Exception e){
			throw e;
		}finally{
			try{
				if(rs!=null){
					if(!rs.isClosed()){
						rs.close();
					}
				}
				if(stat!=null){
					if(!stat.isClosed()){
						stat.close();
					}
				}
			}catch(Exception e){
				
			}
		}
		
		return result;
	}

	public void updateStat(List<String> ids,Connection con) throws Exception{
		if(ids == null || ids.size()==0){
			return ;
		}
		
		PreparedStatement stat = null;
		try{
			StringBuffer idsSb = new StringBuffer("(");
			for(String id:ids){
				idsSb.append("'").append(id).append("',");
			}
			idsSb.deleteCharAt(idsSb.length()-1);
			idsSb.append(")");
			
			StringBuffer update = new StringBuffer();
			update.append(" update t_message ");
			update.append(" set status = ? ");
			update.append(" where id in ").append(idsSb);
			System.out.println("update SQL:"+update.toString());
			stat = con.prepareStatement(update.toString());
			
			stat.setString(1, MessageBean.READED);
			
			stat.execute();
		}catch(Exception e){
			throw e;
		}finally{
			if(stat!=null){
				if(!stat.isClosed()){
					stat.close();
				}
			}
		}
	}

	public int countUnRead(Connection con) throws Exception{
		PreparedStatement stat = null;
		ResultSet rs  = null;
		try{
			StringBuffer sql = new StringBuffer();
			sql.append(" select count(1) from t_message ");
			sql.append(" where status = ? and reciver = ? ");
			
			stat = con.prepareStatement(sql.toString());
			stat.setString(1,MessageBean.UNREADER);
			stat.setString(2,CommonValue.PROPERTIES_MAP.get("current_account"));
			
			rs = stat.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
			else{
				return 0;
			}
			
		}catch(Exception e){
			throw e;
		}finally{
			try{
				if(rs!=null){
					if(!rs.isClosed()){
						rs.close();
					}
				}
				if(stat!=null){
					if(!stat.isClosed()){
						stat.close();
					}
				}
			}catch(Exception e){
				
			}
		}
	}

}
